home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / specfunc / beta.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-18  |  3.9 KB  |  131 lines

  1. /* specfunc/beta.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. /* Author:  G. Jungman */
  21.  
  22. #include <config.h>
  23. #include <gsl/gsl_math.h>
  24. #include <gsl/gsl_errno.h>
  25. #include <gsl/gsl_sf_exp.h>
  26. #include <gsl/gsl_sf_log.h>
  27. #include <gsl/gsl_sf_psi.h>
  28. #include <gsl/gsl_sf_gamma.h>
  29.  
  30. #include "error.h"
  31.  
  32. int
  33. gsl_sf_lnbeta_e(const double x, const double y, gsl_sf_result * result)
  34. {
  35.   /* CHECK_POINTER(result) */
  36.  
  37.   if(x <= 0.0 || y <= 0.0) {
  38.     DOMAIN_ERROR(result);
  39.   }
  40.   else {
  41.     const double max = GSL_MAX(x,y);
  42.     const double min = GSL_MIN(x,y);
  43.     const double rat = min/max;
  44.  
  45.     if(rat < 0.2) {
  46.       /* min << max, so be careful
  47.        * with the subtraction
  48.        */
  49.       double lnpre_val;
  50.       double lnpre_err;
  51.       double lnpow_val;
  52.       double lnpow_err;
  53.       double t1, t2, t3;
  54.       gsl_sf_result lnopr;
  55.       gsl_sf_result gsx, gsy, gsxy;
  56.       gsl_sf_gammastar_e(x, &gsx);
  57.       gsl_sf_gammastar_e(y, &gsy);
  58.       gsl_sf_gammastar_e(x+y, &gsxy);
  59.       gsl_sf_log_1plusx_e(rat, &lnopr);
  60.       lnpre_val = log(gsx.val*gsy.val/gsxy.val * M_SQRT2*M_SQRTPI);
  61.       lnpre_err = gsx.err/gsx.val + gsy.err/gsy.val + gsxy.err/gsxy.val;
  62.       t1 = min*log(rat);
  63.       t2 = 0.5*log(min);
  64.       t3 = (x+y-0.5)*lnopr.val;
  65.       lnpow_val  = t1 - t2 - t3;
  66.       lnpow_err  = GSL_DBL_EPSILON * (fabs(t1) + fabs(t2) + fabs(t3));
  67.       lnpow_err += fabs(x+y-0.5) * lnopr.err;
  68.       result->val  = lnpre_val + lnpow_val;
  69.       result->err  = lnpre_err + lnpow_err;
  70.       result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  71.       return GSL_SUCCESS;
  72.     }
  73.     else {
  74.       gsl_sf_result lgx, lgy, lgxy;
  75.       int stat_gx  = gsl_sf_lngamma_e(x, &lgx);
  76.       int stat_gy  = gsl_sf_lngamma_e(y, &lgy);
  77.       int stat_gxy = gsl_sf_lngamma_e(x+y, &lgxy);
  78.       result->val  = lgx.val + lgy.val - lgxy.val;
  79.       result->err  = lgx.err + lgy.err + lgxy.err;
  80.       result->err += GSL_DBL_EPSILON * (fabs(lgx.val) + fabs(lgy.val) + fabs(lgxy.val));
  81.       result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  82.       return GSL_ERROR_SELECT_3(stat_gx, stat_gy, stat_gxy);
  83.     }
  84.   }
  85. }
  86.  
  87.  
  88. int
  89. gsl_sf_beta_e(const double x, const double y, gsl_sf_result * result)
  90. {
  91.   if(x < 50.0 && y < 50.0) {
  92.     gsl_sf_result gx, gy, gxy;
  93.     gsl_sf_gamma_e(x, &gx);
  94.     gsl_sf_gamma_e(y, &gy);
  95.     gsl_sf_gamma_e(x+y, &gxy);
  96.     result->val  = (gx.val*gy.val)/gxy.val;
  97.     result->err  = gx.err * gy.val/gxy.val;
  98.     result->err += gy.err * gx.val/gxy.val;
  99.     result->err += (gx.val*gy.val)/(gxy.val*gxy.val) * gxy.err;
  100.     result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  101.     return GSL_SUCCESS;
  102.   }
  103.   else {
  104.     gsl_sf_result lb;
  105.     int stat_lb = gsl_sf_lnbeta_e(x, y, &lb);
  106.     if(stat_lb == GSL_SUCCESS) {
  107.       return gsl_sf_exp_err_e(lb.val, lb.err, result);
  108.     }
  109.     else {
  110.       result->val = 0.0;
  111.       result->err = 0.0;
  112.       return stat_lb;
  113.     }
  114.   }
  115. }
  116.  
  117.  
  118. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  119.  
  120. #include "eval.h"
  121.  
  122. double gsl_sf_lnbeta(const double x, const double y)
  123. {
  124.   EVAL_RESULT(gsl_sf_lnbeta_e(x, y, &result));
  125. }
  126.  
  127. double gsl_sf_beta(const double x, const double y)
  128. {
  129.   EVAL_RESULT(gsl_sf_beta_e(x, y, &result));
  130. }
  131.